//
//  bollView3.m
//  footBoll
//
//  Created by cckv on 2017/10/14.
//  Copyright © 2017年 cckv. All rights reserved.
//

#import "bollView3.h"

@interface bollView3()
@property (strong, nonatomic) UIImageView *bgView;
@property (strong, nonatomic) UIImageView *bollView;
@property (strong, nonatomic) UIImageView *doorView;
@property (strong, nonatomic) UIView *arrowView;

@property (strong, nonatomic) UIImageView *item1;
@property (strong, nonatomic) UIImageView *item2;
@property (strong, nonatomic) UIImageView *item3;
@end

@implementation bollView3


#pragma mark - UI
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUI];
    }
    return self;
}

-(void)awakeFromNib
{
    [super awakeFromNib];
    
    [self setUI];
}
-(void)layoutSubviews
{
    [super layoutSubviews];
    self.bgView.frame = CGRectMake(0, 0, kScreenW, kScreenH - 50);
}

- (void)setUI
{
    self.backgroundColor = [UIColor whiteColor];
    
    self.isTouch = NO;
    self.isWin = NO;
    self.index = 0;
    
    UIImageView *bgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH - 50)];
    [self addSubview:bgView];
    self.bgView = bgView;
    bgView.image = [UIImage imageNamed:@"足球.jpg"];
    
    UIImageView *bottomV = [[UIImageView alloc]initWithFrame:CGRectMake(0, kScreenH - 80, kScreenW, 80)];
    [self addSubview:bottomV];
    UIImage *itemImage = [UIImage imageNamed:@"足球_14"];
    bottomV.image = [UIImage resizeWithImage:itemImage];
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self addSubview:backBtn];
    backBtn.frame = CGRectMake(15, 10, 40, 40);
    [backBtn setTitle:NSLocalizedString(@"back", nil) forState:UIControlStateNormal];
    [backBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    
    UIImageView *item2 = [[UIImageView alloc]initWithFrame:CGRectMake(80, kScreenH-130, 80, 80)];
    [self addSubview:item2];
    item2.image = [UIImage imageNamed:@"足球_14"];
    self.item1 = item2;
    
    self.bollView = [[UIImageView alloc] initWithFrame:CGRectMake(100, kScreenH - 170, 40, 40)];
    [self addSubview:self.bollView];
    self.bollView.layer.cornerRadius = 20;
    self.bollView.layer.masksToBounds = YES;
    self.bollView.image = [UIImage imageNamed:@"足球_06"];
    
    UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGestureRecognizer:)];
    self.pan = pan;
    self.bollView.userInteractionEnabled = YES;
    [self.bollView addGestureRecognizer:pan];
    
    
    self.doorView = [[UIImageView alloc] initWithFrame:CGRectMake(kScreenW - 100, kScreenH - 200, 80, 150)];
    [self addSubview:self.doorView];
    self.doorView.image = [UIImage imageNamed:@"足球_03"];
}

#pragma mark - handle
- (void)panGestureRecognizer:(UIPanGestureRecognizer *)pan{
    
    self.isTouch = YES;
    
    CGPoint currentPoint = [pan locationInView:self];
    CGPoint panCentre = pan.view.center;
    CGFloat offsetX = currentPoint.x - panCentre.x;
    CGFloat offsetY = currentPoint.y - panCentre.y;
    CGFloat distance = sqrt(pow(offsetX, 2)+pow(offsetY, 2));
    
    self.arrowView.layer.anchorPoint = CGPointMake(0, 1);
    self.arrowView.layer.transform = CATransform3DMakeRotation(((offsetX) / 180.0 * M_PI), 0, 0, 1);
    
    if (distance >80) {
        CGFloat angle = atan2(offsetY, offsetX);
        offsetX = 80 * cos(angle);
        offsetY = 80 * sin(angle);
    }
    
    self.bollView.transform = CGAffineTransformMakeTranslation(offsetX, offsetY);
    
    if (pan.state == UIGestureRecognizerStateEnded) {
        
        self.isTouch = NO;
        
        UIPushBehavior * push = [[UIPushBehavior alloc]initWithItems:@[self.bollView] mode:UIPushBehaviorModeInstantaneous];
        push.magnitude = distance/80;
        push.angle = atan2(-offsetY, -offsetX);
        [self.animator addBehavior:push];
        
        UIGravityBehavior * gravity = [[UIGravityBehavior alloc]initWithItems:@[self.bollView]];
        gravity.magnitude = 2;
        self.gravity = gravity;
        [self.animator addBehavior:gravity];
        
        UICollisionBehavior * collision = [[UICollisionBehavior alloc]initWithItems:@[self.bollView]];
        collision.translatesReferenceBoundsIntoBoundary = YES;
        collision.collisionDelegate = self;
        
        CGPoint point = self.item1.frame.origin;
        CGPoint point2 = self.item3.frame.origin;
        CGPoint point3 = self.doorView.frame.origin;
        
        [collision addBoundaryWithIdentifier:@"line0" forPath:[UIBezierPath bezierPathWithRect:CGRectMake(0, kScreenH-50, kScreenW, 50)]];
        
        [collision addBoundaryWithIdentifier:@"line1" forPath:[UIBezierPath bezierPathWithRect:CGRectMake(point.x, point.y, self.item1.frame.size.width, self.item1.frame.size.height + 40)]];
        [collision addBoundaryWithIdentifier:@"line2" forPath:[UIBezierPath bezierPathWithRect:CGRectMake(point2.x, point2.y, self.item3.frame.size.width, self.item3.frame.size.height)]];
        
        UIBezierPath *path = [[UIBezierPath alloc] init];
        [path moveToPoint:point3];
        [path addLineToPoint:CGPointMake(point3.x + self.doorView.frame.size.width-20, point3.y+10)];
        [path addLineToPoint:CGPointMake(point3.x + self.doorView.frame.size.width,point3.y + self.doorView.frame.size.height/2)];
        [path addLineToPoint:CGPointMake(point3.x+ self.doorView.frame.size.width, point3.y + self.doorView.frame.size.height)];
        [path addLineToPoint:CGPointMake(point3.x, point3.y + self.doorView.frame.size.height)];
        [path addLineToPoint:CGPointMake(point3.x, point3.y+ self.doorView.frame.size.height/2)];
        [path addLineToPoint:CGPointMake(point3.x+ self.doorView.frame.size.width, point3.y+ self.doorView.frame.size.height/2)];
        [collision addBoundaryWithIdentifier:@"line3" forPath:path];
        
        [self.animator addBehavior:collision];
        
        [self.bollView removeGestureRecognizer:self.pan];
        
    }
}

- (void)collisionBehavior:(UICollisionBehavior*)behavior beganContactForItem:(id <UIDynamicItem>)item withBoundaryIdentifier:(nullable id <NSCopying>)identifier atPoint:(CGPoint)p
{
    
    CGPoint point3 = self.doorView.frame.origin;
    
    CGMutablePathRef pathRef=CGPathCreateMutable();
    CGPathMoveToPoint(pathRef, NULL, point3.x, point3.y);
    CGPathAddLineToPoint(pathRef, NULL, point3.x + self.doorView.frame.size.width-20, point3.y+10);
    CGPathAddLineToPoint(pathRef, NULL, point3.x + self.doorView.frame.size.width, point3.y + self.doorView.frame.size.height/2);
    CGPathAddLineToPoint(pathRef, NULL, point3.x, point3.y + self.doorView.frame.size.height/2);
    CGPathCloseSubpath(pathRef);
    
    if (CGPathContainsPoint(pathRef, NULL, p, NO))
    {
        NSLog(@"point in path!");// 成功了
        
        if (!self.isWin) {
            if ([self.delegate respondsToSelector:@selector(winThisGame)]) {
                self.isWin = YES;
                self.bollView.alpha = 0.6;
                [self.delegate winThisGame];
                
                dataModel *model = [dataModel findByPK:2];
                model.pass = YES;
                [model update];
                
            }
        }
    }
}
- (void)collisionBehavior:(UICollisionBehavior*)behavior endedContactForItem:(id <UIDynamicItem>)item withBoundaryIdentifier:(nullable id <NSCopying>)identifier
{
    
}
- (void)dynamicAnimatorWillResume:(UIDynamicAnimator *)animator{
    
}
- (void)dynamicAnimatorDidPause:(UIDynamicAnimator *)animator{
    
    if (!self.isTouch) {
        if (!self.isWin) {
            if ([self.delegate respondsToSelector:@selector(gameOver)]) {
                [self.delegate gameOver];
            }
        }
        
    }
    
}


#pragma mark - action

- (void)reload{
    
    self.isTouch = YES;
    self.bollView.alpha = 1;
    
    [self.bollView removeFromSuperview];
    self.bollView = nil;
    
    self.bollView = [[UIImageView alloc] initWithFrame:CGRectMake(100, kScreenH - 170, 40, 40)];
    [self addSubview:self.bollView];
    self.bollView.layer.cornerRadius = 20;
    self.bollView.image = [UIImage imageNamed:@"足球_06"];
    self.bollView.layer.masksToBounds = YES;
    self.bollView.userInteractionEnabled = YES;
    
    UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGestureRecognizer:)];
    self.pan = pan;
    [self.bollView addGestureRecognizer:pan];
    
    [self bringSubviewToFront:self.doorView];
    [self.animator removeBehavior:self.gravity];
    
}

@end
